package com.david.documentation.web;

import com.david.documentation.web.view.pages.*;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.settings.IApplicationSettings;
import org.apache.wicket.settings.IResourceSettings;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component(value = "wicketApplication")
public class Application extends WebApplication {

    private static final String DEFAULT_ENCODING = "UTF-8";

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    protected void init() {
        super.init();
        addComponentInstantiationListener(new SpringComponentInjector(this, applicationContext, true));

        getMarkupSettings().setDefaultMarkupEncoding(DEFAULT_ENCODING);
        getRequestCycleSettings().setResponseRequestEncoding(DEFAULT_ENCODING);

        mountBookmarkablePages();
        mountErrorLandingPages();

        if (getConfigurationType().equals(WebApplication.DEPLOYMENT)) {
            getMarkupSettings().setStripWicketTags(false);
            getMarkupSettings().setStripComments(true);
            getMarkupSettings().setCompressWhitespace(true);
            IApplicationSettings applicationSettings = getApplicationSettings();
            applicationSettings.setAccessDeniedPage(ErrorPage.class);
            applicationSettings.setInternalErrorPage(ErrorPage.class);
            applicationSettings.setPageExpiredErrorPage(ErrorPage.class);

        } else {
            IApplicationSettings applicationSettings = getApplicationSettings();
            applicationSettings.setAccessDeniedPage(ErrorPage.class);
            applicationSettings.setInternalErrorPage(ErrorPage.class);
            applicationSettings.setPageExpiredErrorPage(ErrorPage.class);

            getMarkupSettings().setStripWicketTags(false);
            getMarkupSettings().setStripComments(false);
            getMarkupSettings().setCompressWhitespace(true);
        }

        final IResourceSettings rs = getResourceSettings();
        rs.setThrowExceptionOnMissingResource(false);
        rs.addResourceFolder("src/main/resources/i18n");

    }

    private void mountBookmarkablePages() {
        mountBookmarkablePage("error", ErrorPage.class);
        mountBookmarkablePage("home", HomePage.class);
		mountBookmarkablePage("detail", DetailPage.class);
		mountBookmarkablePage("search", SearchResultPage.class);
		mountBookmarkablePage("category", CategoryPage.class);
		mountBookmarkablePage("tags", TagPage.class);
		mountBookmarkablePage("news", NewDokuPage.class);
		mountBookmarkablePage("parser",StartParserPage.class);
    }

    private void mountErrorLandingPages() {
    }

    @Override
    public String getConfigurationType() {
        return WebApplication.DEPLOYMENT;
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return HomePage.class;
    }

    public static Application get() {
        return (Application) WebApplication.get();
    }

    @Override
    public Session newSession(Request request, Response response) {
        Session session = super.newSession(request, response);
        return session;
    }

}